package com.xiezc.fetch.spider.biquge;

import com.xiezc.common.entity.BookInfo;
import lombok.Data;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.time.LocalDate;
import java.util.List;
import java.util.regex.Pattern;

/**
 * http://m.biquge.com.tw/wapsort/1_1.html
 */
@Data
public class BiqugeBookInfoProcess implements PageProcessor {


    private Site site = Site.me()
            .setRetryTimes(3)
            .setSleepTime(100)
            .setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");

    Pattern compile1 = Pattern.compile("/wapbook/\\d+.html");
    Pattern compile = Pattern.compile("/\\d+_\\d+_\\d+_1");

    @Override
    public void process(Page page) {

        int statusCode = page.getStatusCode();
        if (statusCode != 200) {
            return;
        }

        Request request = page.getRequest();
        String url = request.getUrl();
        Html html = page.getHtml();

        //小说介绍详情页
        if (compile1.matcher(url).find()) {
            BookInfo bookInfo = new BookInfo();
            //封面链接
            Selectable info = html.xpath("//div[@class='infohead']");
            String src = info.xpath("//div[@class='pic']/img/@src").get();
            bookInfo.setCoverUrl(src);
            //书名
            String name = info.xpath("//div[@class='cataloginfo']/h3/text()").get();
            bookInfo.setName(name);

            Selectable infotype = info.xpath("//div[@class='cataloginfo']/div[@class='infotype']");
            //作者
            Selectable author = infotype.xpath("//p[1]/text()");
            String authorPre = "作者：";
            String trim = author.get().replaceAll(authorPre, "").trim();
            bookInfo.setAuthor(trim);
            //类型
            Selectable type = infotype.xpath("//p[2]/text()");
            String typePre = "类型：";
            String trimTypePre = type.get().replaceAll(typePre, "").trim();
            bookInfo.setType(trimTypePre);
            //爬取时间
            bookInfo.setCreateTime(LocalDate.now().toString());
            //小说详情页面url
            bookInfo.setFetchUrl(url);

            String desc = info.xpath("//div[@class='intro']/p/text()").get();
            bookInfo.setDesc(desc);

            page.putField("bookInfo", bookInfo);
            return;
        }

        //小说列表页
        if (url.contains("top")) {

            List<String> all = html.css("div.ranking_general").links().all();
            if (all != null && !all.isEmpty()) {
                page.addTargetRequests(all);
            }

            List<String> allPage = html.css("div.page").links().all();
            if (allPage != null && !allPage.isEmpty()) {
                page.addTargetRequests(allPage);
            }
            return;
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

}
